package com.niit.white_box.service;

import org.springframework.stereotype.Component;

import java.time.Instant;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class SmsCodeStore {
    private static class Entry {
        String code;
        long expiresAt;
    }

    private final Map<String, Entry> store = new ConcurrentHashMap<>();

    public void saveCode(String phone, String code, long ttlSeconds) {
        Entry e = new Entry();
        e.code = code;
        e.expiresAt = Instant.now().getEpochSecond() + ttlSeconds;
        store.put(phone, e);
    }

    public boolean verifyCode(String phone, String code) {
        Entry e = store.get(phone);
        if (e == null) return false;
        long now = Instant.now().getEpochSecond();
        if (now > e.expiresAt) return false;
        return e.code.equals(code);
    }

    public void deleteCode(String phone) {
        store.remove(phone);
    }
}



